<template>
  <el-drawer
    ref="drawer"
    size="50%"
    title
    :visible.sync="visibleTopic"
    direction="rtl"
    custom-class="demo-drawer"
    :before-close="cancel"
  >
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item label="题目" prop="content">
        <el-input
          v-model="queryParams.content"
          placeholder="请输入"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table ref="multipleTable" v-loading="loading" :data="courseList" @select="handleSelection" @select-all="handleSelectionAll">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        type="index"
        header-align="center"
        width="50"
        label="序号"
        fixed="left"
        align="center"
      />
      <el-table-column label="题库名称" prop="name" :show-overflow-tooltip="true" />
      <el-table-column label="科目" prop="subject" :show-overflow-tooltip="true" />
      <el-table-column prop="creator" label="创建者" />
      <el-table-column label="创建时间" align="center" prop="createdAt" width="200">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdAt) }}</span>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageIndex"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <div style="position: fixed;bottom: 10px;right: 30px">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-drawer>
</template>
<script>
import { getExerciseList } from '@/api/admin/sys-question'
import { examinationList } from '@/api/admin/sys-examination'
export default {
  name: 'Exercises',
  props: {
    visibleTopic: {
      type: Boolean,
      default: () => {
        return false
      }
    },
    topicType: {
      type: String,
      default: () => {
        return undefined
      }
    },
    idList: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data() {
    return {
      // 查询参数
      queryParams: {
        pageIndex: 1,
        pageSize: 10,
        content: undefined,
        exercise_type: undefined
      },
      loading: false,
      dataList: [],
      total: 0,
      ids: [],
      flagchange: 0,
      courseList: []
    }
  },
  watch: {
    visibleTopic: {
      handler(newVal) {
        if (newVal === true) {
          this.getList()
        }
      }
    },
    topicType: {
      handler(newType) {}
    },
    idList: {
      handler(newVal) {
        this.ids = newVal
      }
    }
  },
  created() {},
  methods: {
    /** 查询参数列表 */
    getList() {
      this.loading = true
      if (this.topicType === 'question_bank') {
        getExerciseList(this.addDateRange(this.queryParams)).then(response => {
          this.courseList = response.data.list
          this.total = response.data.count
          this.loading = false
          this.$nextTick(() => {
            response.data.list.forEach(row => {
              this.ids.forEach(id => {
                if (row.id === id) {
                  this.$refs.multipleTable.toggleRowSelection(row)
                }
              })
            })
          })
        })
      } else {
        examinationList(this.queryParams).then(response => {
          this.courseList = response.data.list
          this.total = response.data.count
          this.loading = false
          this.$nextTick(() => {
            response.data.list.forEach(row => {
              this.ids.forEach(id => {
                if (row.id === id) {
                  this.$refs.multipleTable.toggleRowSelection(row)
                }
              })
            })
          })
        })
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageIndex = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    handleSelectionAll(all) {
      const idList = all.map(item => {
        return item.id
      })
      this.$nextTick(() => {
        this.ids = Array.from(new Set([...idList, ...this.ids]))
      })
    },
    // 多选框选中数据
    handleSelection(selection, row) {
      const selectId = row.id
      if (this.ids.indexOf(selectId) === -1) {
        this.ids.push(selectId)
      } else {
        this.ids = this.ids.filter(item => item !== selectId)
      }
    },
    submitForm() {
      this.$emit('submitFormTopic', this.ids)
    },
    cancel() {
      this.$emit('cancelTopic')
    }
  }
}
</script>
